/* 指定优先级的中缀表达式计算器 */


%{
#define YYSTYPE double
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int yylex (void);
void yyerror (char const *);
%}

%token T_NUM
%token T_LEFT_PT T_RIGHT_PT
%token EOL

%left T_SUB T_ADD
%left T_MUL T_DIV
%left NEG
%right T_POW

%% /* Grammar rules and actions follow. */

input:
	input line
    | /* empty */ {printf("empty\n");}
	;

line: EOL   {printf("newline\n");}
	| exp EOL { printf ("\t%.10g\n", $1); }
	;

exp: T_NUM { $$ = $1; }
	| exp T_ADD exp { $$ = $1 + $3; }
	| exp T_SUB exp { $$ = $1 - $3; }
	| exp T_MUL exp { $$ = $1 * $3; }
	| exp T_DIV exp { $$ = $1 / $3; }
	| T_SUB exp %prec NEG { $$ = -$2; }
	| exp T_POW exp { $$ = pow ($1, $3); }
	| T_LEFT_PT exp T_RIGHT_PT { $$ = $2; }
	;
%%

int main (void)
{
	return yyparse();
}


void yyerror (char const *s)
{
	fprintf (stderr, "%s\n", s);
}